from PyQt4.QtCore import SIGNAL

import time

from network.baseDriver import BaseDriver

import config


def get_time():
    return int(time.time()) % 1e6


def encode_bool(val):
    if val:
        return 1
    else:
        return 0


class GameDriver(BaseDriver):
    # every movement should flush self.step_num
    def __init__(self, parent=None):
        super(GameDriver, self).__init__(parent)
        self.self_name = ''
        self.partner_name = ''
        self.step_num = 0
        self.online = False
        self.role = config.ROLES.vacant

    def timerEvent(self, event):
        if self.online:
            print(self.self_name + ' flush game')
            self.flush_game()

    def new_step(self, x, y):
        values = {
            'username': self.self_name,
            'step_num': self.step_num,
            'time': get_time(),
            'x': x,
            'y': y
        }
        response = self.doRequest('/new_step/', values)

    def retract_ask(self):
        values = {
            'username': self.self_name,
            'step_num': self.step_num,
            'time': get_time()
        }
        response = self.doRequest('/retract_ask/', values)

    def retract_answer(self, permission_argu):
        permission = encode_bool(permission_argu)
        values = {
            'username': self.self_name,
            'step_num': self.step_num,
            'time': get_time(),
            'permission': permission
        }
        response = self.doRequest('/retract_answer/', values)

    def peace_ask(self):
        values = {
            'username': self.self_name,
            'step_num': self.step_num,
            'time': get_time()
        }
        response = self.doRequest('/peace_ask/', values)


    def peace_answer(self, permission_argu):
        permission = encode_bool(permission_argu)
        values = {
            'username': self.self_name,
            'step_num': self.step_num,
            'time': get_time(),
            'permission': permission
        }
        response = self.doRequest('/peace_answer/', values)


    def resign(self):
        values = {
            'username': self.self_name,
            'step_num': self.step_num,
            'time': get_time(),
        }
        response = self.doRequest('/resign/', values)
        self.online = False


    def flush_game(self):
        values = {
            'username': self.self_name,
            'step_num': self.step_num,
            'time': get_time()
        }
        response = self.doRequest('/flush_game/', values)
        print(response)
        action_type = response['action_type']
        if action_type == 'nothing':
            return
        elif action_type == 'new_step':
            x = response['x']
            y = response['y']
            self.online = False
            self.emit(SIGNAL('new_step(int, int)'), x, y)
        elif action_type == 'partner_resign':
            self.online = False
            print('partner resign')
            self.emit(SIGNAL('partner_resign()'))
        elif action_type == 'partner_peace_ask':
            self.online = False
            self.emit(SIGNAL('partner_peace_ask()'))
        elif action_type == 'partner_retract_ask':
            self.online = False
            self.emit(SIGNAL('partner_retract_ask()'))
        elif action_type == 'peace_answer':
            permission = response['permission']
            self.online = False
            self.emit(SIGNAL('peace_answer(int)'), permission)
        elif action_type == 'retract_answer':
            permission = int(response['permission'])
            self.online = False
            self.emit(SIGNAL('retract_answer(int)'), permission)

